<?php

namespace App\Http\Controllers\Wechat;

use App\Model\Rule;
use App\Model\WxReply;
use App\Server\WechatServer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WxReplyController extends Controller
{

    public function index()
    {
//        $ruls = Rule::all();
        $data=WxReply::where('type','text')->get();
//        dd($ruls->toArray());
        return view('wechat.wxReply.index', compact('data'));
    }

    public function create(WechatServer $wechatServer)
    {
        $model = $wechatServer->ruleKey();
        return view('wechat.wxReply.create', compact('model'));
    }


    public function store(Request $request, WechatServer $wechatServer)
    {
//  dd($request->toArray());
//      $data=json_decode($request['content'],true);
//      dd($data);
        \Validator::make($request->all(), [
            'content' => 'required',
        ], [
            'content.required' => '回复内容为空'
        ])->validate();

        $rul = $wechatServer->storeKey($request->all());
        $rul->wxReply()->create(['content' => $request['content'],'type'=>'text']);

        return redirect()->route('wechat.wxReply.index')->with('success','添加成功');
    }

    public function show(WxReply $wxReply)
    {
        //
    }

    public function edit(Rule $wxReply, WechatServer $wechatServer)
    {
//dd($wxReply->toArray());
        $model = $wechatServer->ruleKey($wxReply['id']);
        return view('wechat.wxReply.edit', compact('model','wxReply'));
    }

    public function update(Request $request, Rule $wxReply,WechatServer $wechatServer)
    {
        \Validator::make($request->all(), [
            'content' => 'required',
        ], [
            'content.required' => '回复内容为空'
        ])->validate();
//        $wechatServer->storeKey($request->all());
        $rul = $wechatServer->storeKey($request->all());
        $rul->wxReply()->update(['content' => $request['content']]);
       return redirect()->route('wechat.wxReply.index')->with('success','编辑成功');
    }
    public function destroy(Rule $wxReply)
    {
        $wxReply->delete();
        return back()->with('success','删除成功');
    }

}
